//
//  AppDelegate.m
//  DBAChain
//
//  Created by Evan on 2018/9/4.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginNavigationController.h"
#import "CommonData.h"
#import "AppGuiderView.h"
#import "PMTabBarController.h"
#import "taskHomePageService.h"
#import "UIAlertController+Evan.h"

@interface AppDelegate ()

@end

@implementation AppDelegate



- (void)initView{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    if(![[UserCenter shareCenter] isLogined]){
        [[UserCenter shareCenter] clearUserState];
    }
    
    UIViewController *vc = [UIViewController new];
//    [vc.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"APP引导页"]]];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.window.bounds];
    imageView.image = [UIImage imageNamed:@"APP引导页"];
    [vc.view addSubview:imageView];
    self.window.rootViewController = vc;
    
    //检查版本
    [self checkVersionWithCallback:^{
        // 正常启动
        // 初始化一个tabBar控制器
        PMTabBarController *tb = [[PMTabBarController alloc] init];
        self.window.rootViewController = tb;
    }];
    
    
    //设置Window为主窗口并显示出来
    [self.window makeKeyAndVisible];
    [self setIQKeyboard];
    [NSThread sleepForTimeInterval:3]; 
}

- (void)setIQKeyboard
{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
}

- (void)checkVersionWithCallback:(void(^)(void))callback{
    [DKProgressHUD showLoadingWithStatus:@"检查当前版本"];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:@"ios" forKey:@"appClient"];
    [taskHomePageService getVersion:dict success:^(dataVersion *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            //判断你需要更新的业务逻辑
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // app版本
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            // app build版本
//            NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
            if (![response.data.versioncode isEqualToString: app_Version]) {
                
                [[UIAlertController alloc]alertTitle:@"版本升级" alertMessage:response.data.updatecontent  leftCancelTitle:@"退出" leftCancel:^(UIAlertAction *action) {
                    exit(0);
                } rightTitle:@"立即升级" rightOK:^(UIAlertAction *action) {
                    NSString *textURL = response.data.downloadurl;
                    NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textURL]];
                    [[UIApplication sharedApplication] openURL:cleanURL];
                }];
            }
            
            callback();
        }else{
             callback();
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        callback();
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self initView];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
